package cool.taomu.toolkit.utils

import java.util.Random

class UserAgentUtils {
    val static USER_AGENTS = #[
        // Chrome
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
        // Firefox
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:128.0) Gecko/20100101 Firefox/128.0",
        "Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0",
        // Safari
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/605.1.15",
        "Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1",
        // Edge
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.2592.17",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.2592.17",
        // Opera
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 OPR/109.0.0.0",
        // 移动端 - Android
        "Mozilla/5.0 (Linux; Android 13; SM-G991B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36"
    ]
    static final Random RANDOM = new Random();

    /**
     * 随机获取一个 User-Agent
     */
    def static String getRandomUserAgent() {
        var index = RANDOM.nextInt(USER_AGENTS.size());
        return USER_AGENTS.get(index);
    }

    /**
     * 获取默认 User-Agent（第一个）
     */
    def static String getDefaultUserAgent() {
        return USER_AGENTS.get(0);
    }

    /**
     * 获取指定浏览器类型的 User-Agent（简单示例）
     * @param browserType "chrome", "firefox", "safari", "edge" 等
     * @return 第一个匹配的 User-Agent
     */
    def static String getUserAgentByBrowser(String browserType) {
        for (String ua : USER_AGENTS) {
            if (ua.toLowerCase().contains(browserType.toLowerCase())) {
                return ua;
            }
        }
        return getDefaultUserAgent(); // 默认回退
    }
}
